package com.bawei.module_motocircle.ui.post.detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_motocircle.R
import com.bawei.module_motocircle.databinding.ItemAfterSaleInfoBinding
import com.bawei.module_motocircle.databinding.ItemCommentFirstLevelBinding
import com.bawei.module_motocircle.databinding.ItemCommentFooterBinding
import com.bawei.module_motocircle.databinding.ItemCommentHeaderBinding
import com.bawei.module_motocircle.databinding.ItemCommentSecondLevelBinding
import com.bawei.module_motocircle.databinding.ItemPostDetailBinding
import com.bawei.module_motocircle.ui.post.detail.AfterSalePageItem
import com.bawei.module_motocircle.ui.post.detail.CommentItem
import com.bawei.module_motocircle.ui.post.detail.PostItem

/**
 * @author yaotianxue
 * @description
 * @date 2025/6/3
 */
class PostDetailAdapter(

):RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var postItem:PostItem? = null
    private var afterSale:List<AfterSalePageItem>? = null
    private var comments:List<CommentItem> = mutableListOf()
    private var hasAfterSale:Int = 0


    override fun getItemViewType(position: Int): Int {
        if(hasAfterSale == 1){// 售后 详情 头 评论。。。。 尾
            return when (position) {
                0 -> Type.AFTER_SALE_INFO.value
                1 -> Type.POST.value
                2 -> Type.COMMENTS_HEADER.value
                in 3..comments.size + 2 -> {
                    return if (comments[position - 3].isFirstLevel) Type.FIRST_COMMENTS.value else Type.SECOND_COMMENTS.value
                }
                else -> Type.COMMENTS_FOOTER.value
            }
        }else{
            return when (position) {
                0 -> Type.POST.value
                1 -> Type.COMMENTS_HEADER.value
                in 2..comments.size + 1 -> { //3
                    return if (comments[position - 2].isFirstLevel) Type.FIRST_COMMENTS.value else Type.SECOND_COMMENTS.value
                }
                else -> Type.COMMENTS_FOOTER.value
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            Type.AFTER_SALE_INFO.value -> {
                val binding: ItemAfterSaleInfoBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_after_sale_info,
                    parent,
                    false
                )
                return AfterSaleViewHolder(binding.root)
            }
            Type.POST.value -> {
                val binding: ItemPostDetailBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_post_detail,
                    parent,
                    false
                )
                return PostViewHolder(binding.root)
            }
            Type.COMMENTS_HEADER.value -> {
                val binding: ItemCommentHeaderBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_comment_header,
                    parent,
                    false
                )
                return CommentHeaderViewHolder(binding.root)
            }
            Type.FIRST_COMMENTS.value -> {
                val binding: ItemCommentFirstLevelBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_comment_first_level,
                    parent,
                    false
                )
                return CommentFirstViewHolder(binding.root)
            }
            Type.SECOND_COMMENTS.value -> {
                val binding: ItemCommentSecondLevelBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_comment_second_level,
                    parent,
                    false
                )
                return CommentSecondViewHolder(binding.root)
            }
            else -> {
                val binding: ItemCommentFooterBinding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_comment_footer,
                    parent,
                    false
                )
                return CommentFooterViewHolder(binding.root)
            }
        }

    }


    override fun getItemCount(): Int {
        return hasAfterSale + comments?.size !! + 3

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(getItemViewType(position)){
//            Type.AFTER_SALE_INFO.value -> {
//                val binding = DataBindingUtil.getBinding<ItemAfterSaleInfoBinding>(holder.itemView) ?: return
//
//            }

        }

    }

    fun setPostItem(postItem: PostItem){
        this.postItem = postItem
        notifyDataSetChanged()
    }

    fun setAfterSale(afterSale: List<AfterSalePageItem>){
        this.afterSale = afterSale
        if(afterSale.isNullOrEmpty()){
            hasAfterSale = 1
        }
        notifyDataSetChanged()
    }

    fun setComments(comments: List<CommentItem>){
        this.comments = comments
        notifyDataSetChanged()
    }



    enum class Type(val value: Int) {
        POST(1), COMMENTS_HEADER(2), FIRST_COMMENTS(3), SECOND_COMMENTS(4), COMMENTS_FOOTER(5), AFTER_SALE_INFO(6)
    }

}

class AfterSaleViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
class PostViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
class CommentFirstViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
class CommentSecondViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
class CommentHeaderViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
class CommentFooterViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
